package com.example.worklab.animation.activity

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.graphics.PointF
import android.graphics.drawable.BitmapDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.ViewGroup.LayoutParams
import androidx.core.content.ContextCompat
import com.example.worklab.R
import com.example.worklab.animation.utils.UnReadMsgUtils
import com.example.worklab.animation.widget.ShoppingView
import com.example.worklab.base.BaseActivity
import com.example.worklab.databinding.ActivityShoppingBinding

class ShoppingActivity : BaseActivity<ActivityShoppingBinding>() {

    // 缩放动画
    private var scaleXAnim: ObjectAnimator? = null
    private var scaleYAnim: ObjectAnimator? = null
    // 动画集合
    private var animSet: AnimatorSet? = null

    override val bindingInflater: (LayoutInflater) -> ActivityShoppingBinding
        get() = ActivityShoppingBinding::inflate

    override fun initData(savedInstanceState: Bundle?) {

    }

    override fun initView(savedInstanceState: Bundle?) {
        // 初始化动画
        scaleXAnim = ObjectAnimator.ofFloat(binding.tvDot, "scaleX", 1f, 1.2f, 1f, 1.1f, 1f)
        scaleYAnim = ObjectAnimator.ofFloat(binding.tvDot, "scaleY", 1f, 1.2f, 1f, 1.1f, 1f)
        animSet = AnimatorSet().apply {
            play(scaleXAnim).with(scaleYAnim)
        }

        binding.tvAdd.setOnClickListener {
            // 获取动画起始位置和终点位置坐标
            val iconPos = getScreenPoint(binding.ivIcon)
            val cartPos = getScreenPoint(binding.ivShoppingCart)

            val ivIcon = binding.ivIcon
            // 动画View的大小和商品大小是相同的，
            val lp = LayoutParams(ivIcon.width, ivIcon.height)

            // 创建一个和商品View，大小、图标都相同的View用来做动画
            val shoppingView = ShoppingView(this).apply {
                onAnimEnd = ::onAnimEnd
                setImageBitmap(((ivIcon.drawable) as BitmapDrawable).bitmap)
                layoutParams = lp
            }
            //把 动画View添加到DecoView中，这样才能在屏幕上边移动。
            val decorView = this.window.decorView as ViewGroup
            decorView.addView(shoppingView)

            //执行动画
            shoppingView.start(iconPos, cartPos)
        }
    }

    private fun onAnimEnd() {
        addCount()
        binding.ivShoppingCart.setImageDrawable(
            ContextCompat.getDrawable(
                this,
                R.drawable.shopping_cart_full
            )
        )
    }

    private var count = 0
    private fun addCount() {
        ++count
        if (count <= 0) {
            return
        }
        if (animSet!!.isRunning) {
            animSet!!.cancel()
        }
        animSet?.start()
        binding.tvDot.visibility = View.VISIBLE
        UnReadMsgUtils.show(binding.tvDot, count)
    }

    /**
     * 获取View在屏幕中的坐标
     */
    private fun getScreenPoint(view: View): PointF {
        val location = IntArray(2)
        view.getLocationOnScreen(location)
        return PointF(location[0].toFloat(), location[1].toFloat())
    }
}